<template>
  <div class="course-add addButton">
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>教务管理</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/education/course/list' }">考试课程管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加课程</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="form">
      <el-form ref="form" :model="form" label-width="120px" size="small" :rules="rules">
        <el-form-item label="选择科目:" prop="subject">
          <el-select v-model="form.subject" placeholder="请选择科目" style="width:230px">
            <el-option
              v-for="item in subjectList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考试课程名称:" prop="name">
          <el-input v-model="form.name" placeholder="请输入考试课程名称" style="width:230px"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="background:#ff8b00;color:#fff" @click="onSubmit" :disabled="isDisabled">提交</el-button>
          <el-button style="background:#999999;color:#fff" @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>

</template>
<script>
export default {
  mounted() {
    this.getSubjectList();
  },
  data() {
    return {
      form: {
        subject: "",  // 科目id
        name: "", //考试课程名
      },
      intentionList: [],
      subjectList: [],
      rules: {
        subject: [
          { required: true, message: "请选择科目", trigger: "change" },
        ],
        name: [
          { required: true, message: "请填写考试课程名称", trigger: "blur" },
          { min: 2, max: 50, message: "长度在 2 到 50 个字符", trigger: "blur" }
        ]
      },
      isDisabled: false,
    };
  },
  methods: {
    getSubjectList(){
      this.$api.education.subjectList({
        pageNum:1,
        pageSize:100
      }).then((res)=>{
        if(res.code===200){
          console.log(res);
          this.subjectList = res.data.list;
        }
      })
    },
    onSubmit() {
      let that = this;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          that.isDisabled = true;
          that.$api.education.courseAdd(that.form)
          .then((res)=>{
             if(res.code===200){
               this.isDisabled =false;
               that.$message.success("添加成功")
             }
          })
          .catch(err=>{
            this.isDisabled = false;
          })
        }
      })
    },
    resetForm(formName) {
      this.form = {
        subject: "",
        name: "",
      };
      this.$refs["form"].resetFields();
      this.isDisabled = false;
    }
  },
};
</script>
<style scoped>
.course-add {
  padding: 30px;
}

.course-add .form {
  max-width: 700px;
  margin: 30px 0px;
}

.course-add .img {
  position: absolute;
  top: 50%;
  left: 50%;
  max-width: 360px;
  max-height: 180px;
  transform: translate(-50%, -50%);
  z-index: 0;
}
</style>
